package com.ty.init.service.impl;

import com.ty.init.mapper.UserMapper;
import com.ty.init.pojo.User;
import com.ty.init.service.UserService;
import com.ty.init.service.impl.ex.PasswordNotMatchException;
import com.ty.init.service.impl.ex.UserExistException;
import com.ty.init.service.impl.ex.UserNotFindException;
import com.ty.init.uitl.Vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.*;

/**
 * @author ty
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User login(String username, String password) {
        User result = userMapper.findByUsername(username);
        if (result == null){
            throw new UserNotFindException("用户不存在");
        }
        String salt = result.getSalt();
        String newPassword = getMd5Password(password,salt);
        String oldPassword = result.getPassword();
        if (!newPassword.equals(oldPassword)){
            throw new PasswordNotMatchException("密码错误");
        }
        if (result.getIsDelete() == 1){
            throw new UserNotFindException("用户不存在");
        }
        return result;

    }

    @Override
    public List findUsers() {
        List<User> users = userMapper.findUsers();

        return users;
    }

    @Override
    public List findUsersLimit(Integer start, Integer pageSize) {
        List<User> usersLimit = userMapper.findUsersLimit(start, pageSize);
        return usersLimit;
    }

    @Override
    public void addUser(User user) {
        String username = user.getUsername();
        User result = userMapper.findByUsername(username);
        if (result != null){
            throw new UserExistException("用户已存在");
        }
        String oldPassword = user.getPassword();
        String salt = UUID.randomUUID().toString().toUpperCase();
        String newPassword = getMd5Password(oldPassword,salt);
        user.setPassword(newPassword);
        user.setIsDelete(0);
        Date date = new Date();
        user.setSalt(salt);
        user.setCreatedTime(date);
        user.setModifiedTime(date);
        user.setCreatedUser(user.getUsername());
        user.setModifiedUser(user.getUsername());
        Integer user1 = userMapper.addUser(user);
        if (user1 != 1){
            throw new UserExistException("添加失败");
        }
    }

    @Override
    public Integer deleteUser(Integer uid) {
        Integer result = userMapper.deleteUser(uid);
        return result;
    }

    @Override
    public User findById(Integer uid) {
        User byId = userMapper.findById(uid);
        return byId;
    }

    @Override
    public Integer userUpdate(User user, String username) {
        user.setModifiedUser(username);
        Date date = new Date();
        user.setModifiedTime(date);
        Integer data = userMapper.updateMessage(user);
        return data;
    }

    @Override
    public Integer changePassword(Integer uid,String password,String info ) {
        User user = userMapper.findById(uid);
        String oldPassword = user.getPassword();
        String newPassword = getMd5Password(password,user.getSalt());
        if (!oldPassword.equals(newPassword)){
            throw new PasswordNotMatchException("密码不正确");
        }
        String nowPassword = getMd5Password(info,user.getSalt());
        Integer integer = userMapper.changePassword(uid, nowPassword);
        return integer;
    }

    @Override
    public List<UserVo> userMessage() {
        List<UserVo> result = userMapper.userMessage();

        return result;
    }


    private String getMd5Password(String password,String salt){
        for (int i = 0; i < 3; i++) {
            password = DigestUtils.md5DigestAsHex((salt + password +salt).getBytes()).toUpperCase();
        }
        return password;
    }
}
